package com.oauth.google.client.ui.gmail;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.google.client.beans.gmail.GoogleMail;
import com.oauth.google.client.beans.gmail.GoogleMailModel;

public class HomePanel extends Composite {

	private static HomePanelUiBinder uiBinder = GWT
			.create(HomePanelUiBinder.class);

	interface HomePanelUiBinder extends UiBinder<Widget, HomePanel> {
	}

	@UiField
	VerticalPanel mailListPanel;

	public HomePanel() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	private VerticalPanel getMail(final GoogleMail mail) {
		// hPanel.add(new Image(pTweet.getUser().getProfileImageUrl()));
		VerticalPanel vPanel = new VerticalPanel();
		Anchor anchor = new Anchor(mail.getSubject(), true);
		anchor.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent pEvent) {
				Window.open(mail.getLink(), mail.getSubject(), null);
			}

		});
		vPanel.setSpacing(4);
		vPanel.add(anchor);
		return vPanel;
	}

	public void setData(GoogleMailModel pModel) {
		mailListPanel.clear();
		for (GoogleMail mail : pModel.getMailList()) {
			mailListPanel.add(getMail(mail));
		}
	}
}
